<?php


namespace App\Shuixing\Controllers;


use App\Models\Posts\WxComment;
use App\Models\Posts\WxLike;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsImg;
use App\Models\Posts\WxPostsTag;
use App\Models\Posts\WxPostsVideo;
use App\Models\Posts\WxTag;
use App\Models\Posts\WxUserCircle;
use App\Models\WxCircle;
use App\Models\WxUser;
use App\Models\WxUserFollow;
use Illuminate\Http\Request;

class SearchController
{
    public function index($id, $keyword = '')
    {
        //帖子
        $pdata = WxPost::where('is_examine', 1)
            ->where('posts_state', 0)
            ->where('posts_content', 'like', '%' . $keyword . '%')
            ->orderBy('is_sticky', 'desc')
            ->orderBy('id', 'desc')
            ->get();
        $pdata->map(function ($v) {
            $v->user = WxUser::where('id', $v->user_id)
                ->first(['user_avatar', 'user_name', 'id', 'is_member', 'is_official', 'is_authentication']);//发帖人
            $tagsIds = WxPostsTag::where('posts_id', $v->id)->pluck('tags_id');//标签ID列表
            $v->tags = WxTag::whereIn('id', $tagsIds)->get(['tags_name', 'id']);//标签列表
            $v->like_count = WxLike::where('posts_id', $v->id)->count(); // 帖子喜欢数量
            $v->comment_count = WxComment::where(['posts_id' => $v->id, 'comment_state' => 1])->count(); // 评论数量
            $v->image = WxPostsImg::where('posts_id', $v->id)->value('img_url'); // 帖子图片
            $v->vimage = WxPostsVideo::where('posts_id', $v->id)->value('video_thumb_url'); // 帖子视频封面
            $v->format_time = format_datetime($v->created_at); // 发帖时间
        });

        //圈子
        $cdata = WxCircle::where('circle_state', 1)
            ->where(function ($query) use ($keyword) {
                $query->orWhere('circle_name', 'like', '%' . $keyword . '%')
                    ->orWhere('circle_introduce', 'like', '%' . $keyword . '%');
            })
            ->orderBy('sort', 'asc')
            ->get();
        $cdata->map(function ($v) {
            $v->user_count = WxUserCircle::where('circle_id', $v->id)->count();
            $v->posts_count = WxPost::where('circle_id', $v->id)->count();
        });

        //用户
        $udata = WxUser::where(function ($q) use ($keyword) {
            $q->where('user_name', 'like', '%' . $keyword . '%');
        })
            ->get(['id', 'user_name', 'user_avatar', 'user_introduce']);
        $udata->map(function ($v) {
            $v->posts_count = WxPost::where('user_id', $v->id)->count();
            $v->fans_count = WxUserFollow::where('user_follow_id', $v->id)->count();
        });

        $data = [];
        $data['posts_count'] = WxPost::where('is_examine', 1)
            ->where('posts_state', 0)
            ->where('posts_content', 'like', '%' . $keyword . '%')
            ->count();
        $data['circle_count'] = WxCircle::where('circle_state', 1)
            ->where(function ($query) use ($keyword) {
                $query->orWhere('circle_name', 'like', '%' . $keyword . '%')
                    ->orWhere('circle_introduce', 'like', '%' . $keyword . '%');
            })
            ->count();
        $data['user_count'] = WxUser::where(function ($q) use ($keyword) {
            $q->where('user_name', 'like', '%' . $keyword . '%');
        })->count();

        return view('shuixing/search', ['data' => $data, 'pdata' => $pdata, 'cdata' => $cdata, 'udata' => $udata, 'keyword' => $keyword, 'type' => $id]);
    }
}
